import json
from absl import app


def merge(files, out):
  mea = None
  lists = []
  for f in files:
    with open(f) as fd:
      doc = json.load(fd)
      mea = doc['mea']
      lists.extend(doc['product_infos'])

  doc = {
      "mea": mea,
      "product_infos": lists,
  }
  with open(out, 'w') as fd:
    json.dump(doc, fd, indent=2)


def main(argv):
  merge(['Futures.Huobi.v1.json', 'Futures.Huobi.v1-swap.json', 'Spot.Huobi.v1.json',
         'Futures.Huobi.v1-linear-swap.json'],
        'old_compat/huobi.json')
  merge(['Futures.Kraken.v3.json', 'Spot.Kraken.v1.json'], 'old_compat/kraken.json')
  merge(['Futures.Quoinex.v2.json', 'Spot.Quoinex.v2.json'], 'old_compat/quoinex.json')
  merge(
      ['Futures.Binance.v1.json', 'Spot.Binance.v1.json', 'Futures.Binance.v1-delivery.json'],
      'old_compat/binance.json')
  merge(['Futures.Okex.v5.json', 'Futures.Okex.v5-swap.json', 'Spot.Okex.v5.json'],
        'old_compat/okex.json')


if __name__ == '__main__':
  app.run(main)
